﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LybrixNursingCommon;
using LybrixNursingCRMDesktop.FrameWork;
using System.Configuration;
using System.Globalization;
using System.Reflection;
namespace LybrixNursingCRMDesktop
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        private readonly LybrixNursingCommon.NursingDBContainer _entities;
        public static agent loggedUser = null;
        public static bool isArabic = true;
        public Login()
        {
        
            InitializeComponent();
            _entities = Repository.Entities;

            #region Try Load logo & Library Name

            var uri =
                new Uri(string.Format("{0}\\SoftwareLogo\\logo.jpg",
                                      System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
            var bitmap = new BitmapImage(uri);
            libraryLogo.Source = bitmap;

            SoftwareName.Content =ConfigurationManager.AppSettings["SoftwareName"].ToString(CultureInfo.InvariantCulture);
            SoftwareVersion.Content = Assembly.GetExecutingAssembly().GetName().Version;
            #endregion
        }

        private void BtnCancelClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnLoginClick(object sender, RoutedEventArgs e)
        {
            bool valid = false;
            SetHebrewDictionary();

            valid = CheckLoginAuthentication(txtUserName.Text, txtPassword.Password);
            if (valid)
            {
                LoadApplication();

            }
            else
            {
                MessageBox.Show("שם משתמש או סיסמה אינם נכונים", "שגיאה", MessageBoxButton.OK);
                return;
            }

        }

        private void LoadApplication()
        {
            var win = new StartWindow();
            win.Show();
            win.WindowState = WindowState.Maximized;
            Close();
        }

        private void RemoveCurrentDictionaries()
        {
            //Detect Languages Dictionaries
            List<ResourceDictionary> dicToRemove = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                if (dictionary.Source.ToString().Contains("UIArabic.xaml") ||
                    dictionary.Source.ToString().Contains("MessagesArabic.xaml"))
                    dicToRemove.Add(dictionary);

                if (dictionary.Source.ToString().Contains("UIHebrew.xaml") ||
                    dictionary.Source.ToString().Contains("MessagesHebrew.xaml"))
                    dicToRemove.Add(dictionary);
            }
            //Remove the Dictionaries
            foreach (ResourceDictionary dictionary in dicToRemove)
                Application.Current.Resources.MergedDictionaries.Remove(dictionary);
        }
        private void SetHebrewDictionary()
        {
            isArabic = false;
            RemoveCurrentDictionaries();

            ResourceDictionary dictUI = new ResourceDictionary();
            dictUI.Source = new Uri("Resources\\UIHebrew.xaml", UriKind.Relative);

            ResourceDictionary dictMessages = new ResourceDictionary();
            dictMessages.Source = new Uri("Resources\\MessagesHebrew.xaml", UriKind.Relative);

            Application.Current.Resources.MergedDictionaries.Add(dictUI);
            Application.Current.Resources.MergedDictionaries.Add(dictMessages);

        }

        private bool CheckLoginAuthentication(string user , string pass)
        {
            bool valid = false;
            agent loggedagent = null;
            IQueryable<agent> agents = null;
            try
            {
                agents = (from x in _entities.agents where x.userName == user select x);
                if (agents.Count<agent>() > 0)
                {
                    loggedagent = agents.First<agent>();
                    loggedUser = loggedagent;
                    if (loggedagent.password == pass)
                        valid = true;
                    else
                        valid = false;
                }
                else
                    valid = false;
                return valid;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "שגיאה", MessageBoxButton.OK);
                return false;
            }
        }
    }
}
